﻿using ERPSystem.Common.Domains.CustomerProfileCommon;
using ERPSystem.Common.Domains.PurchaseInvoiceCommon;
using ERPSystem.Common.DTO;
using ERPSystem.Enum.CommonErrorCodes;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using SalesManagementCommon.API.SalesManagementCommons.PurchaseInvoiceRead;
using SalesManagementCommon.API.SalesManagementCommons.PurchaseReturnOrderRead;
using SalesManagementCommon.API.SalesManagementCommons.SalesManagementSerach;
using SalesManagementCommon.API.SalesManagementCommons.SalesOrderSearch;
using SqlSugar;

namespace SalesManagementCommon.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class SalesOrderController : ControllerBase
    {
        private readonly IMediator _mediator;

        public SalesOrderController(IMediator mediator)
        {
            _mediator = mediator;
        }

        /// <summary>
        /// 客户信息搜索
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultPage<CustomerProfile>> SearchCustomerProfile(CustomerProfileSearchCommand request, CancellationToken cancellationToken)
        {
            return await _mediator.Send(request, cancellationToken);
        }
        /// <summary>
        /// 处理销售订单单据显示查询命令
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Result<PageResultDto<SalesOrderSearchDTO>>> SearchSalesOrderDetail([FromQuery] SalesOrderSearchCommand request, CancellationToken cancellationToken)
        {
            return await _mediator.Send(request, cancellationToken);
        }
        /// <summary>
        /// 处理销售订单单据查询请求
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResultPage<SalesOrderDTO>> QuerySalesOrder([FromQuery] SalesOrderQueryCommand request, CancellationToken cancellationToken)
        {
            return await _mediator.Send(request, cancellationToken);
        }
    
        /// <summary>
        /// 采购发票
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Result<PageResultDto<PurchaseInvoiceReadQueryDto>>> PurchaseInvoiceReadQuery([FromQuery] PurchaseInvoiceReadQueryCommand request, CancellationToken cancellationToken)
        {
            var result = await _mediator.Send(request);
            return result;
        }
        /// <summary>
        /// 查询发票类型
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Result> InvoiceTypeQuery([FromQuery]InvoiceTypeQueryCommand request, CancellationToken cancellationToken)
        {
            var result = await _mediator.Send(request);
            return result;
        }
        /// <summary>
        /// 获取采购退货单
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Result<PageResultDto<PurchaseReturnOrderQueryDto>>> PurchaseReturnOrderQuery([FromQuery]PurchaseReturnOrderQueryCommand request, CancellationToken cancellationToken)
        {
            var result = await _mediator.Send(request);
            return result;
        }
        /// <summary>
        /// 查看采购退货单数据
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Result<PurchaseReturnOrderBackFillDto>> PurchaseReturnOrderBackFill([FromQuery] PurchaseReturnOrderBackFillCommand request, CancellationToken cancellationToken)
        {

            var result = await _mediator.Send(request);
            return result;
        }
        /// <summary>
        /// 采购退货单导出
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<FileExportResult> PurchaseReturnOrderReadXLS([FromQuery]PurchaseReturnOrderReadXLSCommand request, CancellationToken cancellationToken)
        {
            var result = await _mediator.Send(request);
            return result;
        }
    }
}
